package dao

import (
	"gostore/model"
)

// 用户订单
type ResUserStoreOrder struct {
	Id         int     `json:"id"`         // 订单id
	OrderId    string  `json:"orderId"`    // 订单号
	Uid        int     `json:"uid"`        // 用户id
	RealName   string  `json:"realName"`   // 用户姓名
	TotalNum   int     `json:"totalNum"`   // 订单商品总数
	TotalPrice float64 `json:"totalPrice"` // 订单总价
	PayPrice   float64 `json:"payPrice"`   // 付款金额
}

// 获取用户订单
func GetUserStoreOrderList(uid int, req *Query) (r []*ResUserStoreOrder, count int64, err error) {

	db := GetDB()

	limit := 10
	page := 0

	if req.Page > 0 {
		page = req.Page - 1
	}
	if req.PageNum > 0 {
		limit = req.PageNum
	}

	offset := limit * page

	res := make([]*model.StoreOrder, 0)

	db = db.Select("id, order_id, uid, real_name, total_num, total_price, pay_price").Where("uid = ?", uid)
	err = db.Offset(offset).Limit(limit).Find(&res).Count(&count).Error
	if err != nil {
		return
	}

	for _, item := range res {
		r = append(r, &ResUserStoreOrder{
			Id:         item.Id,
			OrderId:    item.OrderId,
			Uid:        item.Uid,
			RealName:   item.RealName,
			TotalNum:   item.TotalNum,
			TotalPrice: item.TotalPrice,
			PayPrice:   item.PayPrice,
		})
	}
	return
}
